﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using CJ_Logging.Configuration.Config;

namespace CJ_Logging.Configuration
{
    public class LoadConfig
    {
        private static LoadConfig m_LoadConfig = null;

        private SystemConfig SystemConfig { get; set; }
        private SaveExceptionInfoConfig SaveExceptionInfoConfig { get; set; }
        private SendMailConfig SendMailConfig { get; set; }
        private FileLogConfig FileLogConfig { get; set; }

        public static LoadConfig Instance
        { 
            get
            {
                if (m_LoadConfig == null)
                    m_LoadConfig = new LoadConfig();
                return m_LoadConfig;
            }
        }

        /// <summary>
        /// 读取系统配置
        /// </summary>
        /// <returns></returns>
        public SystemConfig LoadSystemConfig()
        {
            SystemConfig config = new SystemConfig();
            if (SystemConfig == null || SystemConfig.ExpiredTime < DateTime.Now)
            {
                NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig("SystemConfig");
                config.ConfigCacheTime = Convert.ToInt32(nc["ConfigCacheTime"]);
                config.ExpiredTime = DateTime.Now.AddSeconds(config.ConfigCacheTime);
                SystemConfig = config;
            }
            else
            {
                config = SystemConfig;
            }
            return config;
        }

        /// <summary>
        /// 读取存储异常信息的配置
        /// </summary>
        /// <returns></returns>
        public SaveExceptionInfoConfig LoadSaveExceptionInfoConfig()
        {
            SaveExceptionInfoConfig config = new SaveExceptionInfoConfig();
            if (SaveExceptionInfoConfig == null || SaveExceptionInfoConfig.ExpiredTime < DateTime.Now)
            {
                NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig("ExceptionConfiguration/SaveExceptionInfo");
                config.IsWriteFile = Convert.ToBoolean(nc["IsWriteFile"]);
                config.IsSendMail = Convert.ToBoolean(nc["IsSendMail"]);
                config.ExpiredTime = DateTime.Now.AddSeconds(LoadSystemConfig().ConfigCacheTime);
                SaveExceptionInfoConfig = config;
            }
            else
            {
                config = SaveExceptionInfoConfig;
            }
            return config;
        }

        ///// <summary>
        ///// 读取写入文件LOG配置
        ///// </summary>
        ///// <returns></returns>
        //public FileLogConfig LoadFileLogConfig()
        //{
        //    FileLogConfig config = new FileLogConfig();
        //    if (FileLogConfig == null || FileLogConfig.ExpiredTime < DateTime.Now)
        //    {
        //        NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig("ExceptionConfiguration/WriteFile");
        //        config.IsWriteFile = Convert.ToBoolean(nc["IsWriteFile"]);
        //        config.ExpiredTime = DateTime.Now.AddSeconds(LoadSystemConfig().ConfigCacheTime);
        //        FileLogConfig = config;
        //    }
        //    else
        //    {
        //        config = FileLogConfig;
        //    }
        //    return config;
        //}

        /// <summary>
        /// 读取发邮件的配置
        /// </summary>
        /// <returns></returns>
        public SendMailConfig LoadSendMailConfig()
        {
            SendMailConfig config = new SendMailConfig();
            if (SendMailConfig == null || SendMailConfig.ExpiredTime < DateTime.Now)
            {
                NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig("ExceptionConfiguration/SendMail");
                config.MailTo = nc["MailTo"];
                config.MailFrom = nc["MailFrom"];
                config.MailDisplayName = nc["MailDisplayName"];
                config.MailTitle = nc["MailTitle"];
                config.SmtpHost = nc["SmtpHost"];
                config.SmtpUserName = nc["SmtpUserName"];
                config.SmtpPassword = nc["SmtpPassword"];
                config.IsSendAsync = Convert.ToBoolean(nc["IsSendAsync"]);
                config.IsBodyHtml = Convert.ToBoolean(nc["IsBodyHtml"]);
                config.ExpiredTime = DateTime.Now.AddSeconds(LoadSystemConfig().ConfigCacheTime);
                SendMailConfig = config;
            }
            else
            {
                config = SendMailConfig;
            }
            return config;
        }
    }
}
